package com.root.dreameraadmin.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.root.dreameraadmin.R;
import com.root.dreameraadmin.utils.AsyncGetDataUtil;
import com.root.dreameraadmin.utils.AsyncImageLoader;

import java.util.List;

/**
 * Created by acer on 2016/9/19.
 */

public class PictureAdapter extends BaseAdapter {


    private static final String TAG="PictureAdapter";
    private AsyncImageLoader asyncImageLoader;
    List<String> data, title_data,pic_id,datetime_data,detail_title_data;
    Context context;

    ImageView picture_iv;
    TextView picture_title,picture_datetime,picture_detail_title;
//    Button update_submit;
    private ListView listView;

    public PictureAdapter(Context context, List<String> list, List<String> title, List<String> pic_id,
                          List<String> datetime_data,List<String> detail_title_data,ListView listView){
        this.context=context;
        this.data=list;
        this.title_data=title;
        this.pic_id=pic_id;
        this.datetime_data=datetime_data;
        this.detail_title_data=detail_title_data;
        asyncImageLoader=new AsyncImageLoader();
        this.listView=listView;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.definite_listview,
                    null);
        }
        picture_iv= (ImageView) convertView.findViewById(R.id.picture_view);

        Bitmap pic_bitmap= AsyncGetDataUtil.getPicFromFile(pic_id.get(position).toString());
        Log.d(TAG,"pic_id is"+pic_id.get(position).toString());
        if(pic_bitmap!=null){
            picture_iv.setTag(pic_id);
            picture_iv.setImageBitmap(pic_bitmap);
        }else {

            String url = data.get(position).toString();
            Log.d(TAG, "url is " + url);
            picture_iv.setTag(url);
            Drawable cachedImage = asyncImageLoader.loadDrawable(url, new AsyncImageLoader.ImageCallback() {
                public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                    ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
                    if (imageViewByTag != null) {
                        imageViewByTag.setImageDrawable(imageDrawable);
                    }
                }
            });
            if (cachedImage == null) {
                picture_iv.setImageResource(R.drawable.empty_photo);
            } else {
                picture_iv.setImageDrawable(cachedImage);
            }
        }
        picture_title= (TextView) convertView.findViewById(R.id.picture_title);
        picture_title.setText(title_data.get(position).toString());
        picture_datetime= (TextView) convertView.findViewById(R.id.picture_datetime);
        picture_datetime.setText(datetime_data.get(position).toString());
        picture_detail_title= (TextView) convertView.findViewById(R.id.picture_detail_title);
        picture_detail_title.setText(detail_title_data.get(position).toString());
        return convertView;
    }
}
